{extend name="layout/default" /}
{block name="head"}


<title>插件列表</title>

{/block}
{block name="content"}
<div class="layui-card">
	<!--模糊搜索区域-->
	<!--数据表格-->
	<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
		<ul class="layui-tab-title">
			<li class="layui-this">全部插件</li>
			<li>本地插件</li>
		</ul>

		<div class="layui-tab-content">
			<div class="layui-tab-item layui-show">
	<!--<script type="text/html" id="toolbarTpl">-->
	<!--	<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">-->
	<!--		<i class="layui-icon layui-icon-add-1"></i>-->
	<!--		新增-->
	<!--	</button>-->
		
	<!--		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">-->
	<!--		<i class="layui-icon layui-icon-add-1"></i>-->
	<!--		新增2-->
	<!--	</button>-->
	<!--		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">-->
	<!--		<i class="layui-icon layui-icon-add-1"></i>-->
	<!--		刷新-->
	<!--	</button>-->
	
	<!--</script>-->
				<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
			</div>

			<div class="layui-tab-item">
				<table class="layui-hide" id="tableIdbd" lay-filter="tableFilter"></table>
			</div>
		</div>

	</div>
</div>
{/block}
{block name="scripts"}
<!--js逻辑-->
<script>
	layui.use(["jquery", "table", "form", "laydate"], function () {
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;

		let $ = layui.jquery;


		//全部插件
		let userTable = table.render({
			elem: '#tableId',
			url: '{:url("getAllplug")}',
			cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
			limit: 10000,
			page: true,
			defaultToolbar: [],
			toolbar: "#toolbarTpl",
			height: 'full',
			cols: [[
				// {field: "intro", title: "标识", width: 100, sort: true},
				{ field: "title", title: "名称" },
				{ field: "version", title: "版本", width: 80 },
				{ field: "description", title: "简述", width: 420 },
				{ field: "author", title: "作者", width: 80 },
				{ field: "price", title: "价格", width: 80 },
				{ field: "sales", title: "销量", width: 80 },
				{ field: "status", title: "状态", templet: "#statusTpl", width: 120 },

				{ field: "button", title: "操作按钮", minWidth: 180 },
			]],
			done: function (res, curr, count) {
				console.info(res, curr, count);
			}
		});

		//本地插件
		let userTablebd = table.render({
			elem: '#tableIdbd',
			url: '{:url("index")}',
			cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
			limit: 10000,
			page: true,
		defaultToolbar: [{
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}],
			toolbar: "#toolbarTpl",
			height: 'full',
			cols: [[
				//{field: "name", title: "标识", width: 100, sort: true},
				{ field: "title", title: "名称", width: 200 },
				{ field: "version", title: "版本", width: 80 },
				{ field: "description", title: "简述", width: 420 },
				{ field: "author", title: "作者", width: 100 },

				{ field: "status", title: "状态", width: 120, templet: "#statusTpl" },

				{ field: "button", width: 180, title: "操作按钮" },
			]],
			done: function (res, curr, count) {
				console.info(res, curr, count);
			}
		});

		form.on("submit(search)", function (data) {
			userTable.reload({
				where: data.field,
				page: { curr: 1 }
			});
			return false;
		});
		//监听性别操作
		form.on('switch(status)', function (obj) {
			var swt = $(obj.elem);
			swt.prop('checked', !obj.elem.checked);
			layui.form.render();//刷新表格
			state(obj.value, obj.elem)
		});
		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "batchEnabled":
					batchEnabled();
					break;
				case "batchDisabled":
					batchDisabled();
					break;
				case "batchDel":
					batchDel();
					break;
				case "refresh":
					window.refresh();
					break;
				case "add":
					add();
					break;
			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "install":
					install(data.name);
					break;
				case "uninstall":
					uninstall(data.name);
					break;
				case "config":
				// 	config(data.name);
					 window.config(data.name);
					break;
				case "installyuancheng":
					installyuancheng(data.name);
					break;
			}
		});
		
			window.refresh = function(param) {
			     layer.close(loading);
					table.reload('tableIdbd');
				}

		//安装远程插件
		function installyuancheng(id) {
			const Storage = {};
			Storage.get = function (name) {
				return localStorage.getItem(name);
			}
			var caomei_tokeny = Storage.get("caomeiadmin_userinfo");
			layer.confirm("确定要安装吗？", function () {
			layer.close(id);
			let loading = layer.load();
				$.ajax({
				
			            	url:'{:url('installyuancheng')}',
                            data:{id: id, token: caomei_tokeny},
                            dataType: 'json',
                            type: 'get',
				
				}).done(function (response) {
					if (response.code == '1') {
					    console.log(1111);
						console.log(response);
						layer.close(loading);
						//userTable.reload();
						//okUtils.tableSuccessMsg(response.msg);
				// 		okLayer.greenTickMsg(response.msg, function () {
						
				// 		})
						layer.msg(response.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function() {
                                     	userTable.reload();
							            userTablebd.reload();
                                    });
					} else {

						if (response.code == '-3' || response.code == '-4') {
						    	layer.close(loading);
							//未登录
							layer.msg(response.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function () {
								layer.open({
									type: 2,
									title: '用户登录',
									maxmin: true,
									skin: 'layui-layer-lan',
									shadeClose: true, //点击遮罩关闭层
									area: ['600px', '480px'],
									content: '{:url("caomei/login")}'//弹框显示的url
								});
							});
						} else {
						    	layer.close(loading);
							layer.msg(response.msg, {
                                        icon: 2,
                                        time: 1000
                                    },  function () {
							})
						}


						if (response.code == '-5') {
						    	layer.close(loading);
							//未购买
							layer.msg(response.msg, {
                                        icon: 5,
                                        time: 1000
                                    },  function () {
								var html = "<img style='height: 200px;width: 200px;' src='" + response.data.img + "' >";
								layer.open({
									type: 2,
									title: '请使用手机扫码二维码支付',
									shade: [0.001, '#393D49'],
									area: ['500px', '500px'],
									// skin : 'layui-layer-nobg', //沒有背景色
									shadeClose: true,
									content: '{:url("caomei/payqr")}?img=' + response.data.code_url + '&order_sn=' + response.data.order_sn + '&name=' + response.data.name //弹框显示的url
								});
							});
						} else {
						    	layer.close(loading);
							layer.msg(response.msg, {
                                        icon: 2,
                                        time: 1000
                                    },  function () {
							})
						}

					}

				}).fail(function (error) {
				    layer.close(loading)
					console.log(error)
					layer.alert(error.responseText, { title: "安装错误", icon: 2, anim: 4 })
				});
			})
		}

		//安装本地插件
		function install(id) {
			layer.confirm("确定要安装吗？", function () {
			layer.close(id);
			let loading = layer.load();
			 $.ajax({
                            url:'{:url('install')}',
                            data:{id:id},
                            dataType: 'json',
                            type: 'get',
                            success: function(res) {
                                layer.close(loading);
                                //判断有没有权限
                                if(res && res.code==999){
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        time: 2000, 
                                    })
                                    return false;
                                }else if (res.code==1) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function() {
                                      table.reload('tableIdbd');
                                      table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2,
                                        time: 1000
                                    });
                                }
                            }
                        }).fail(function (error) {
					console.log(error)
				});
			})
		}

// 		function config(id) {
// 			okLayer.open("配置", "config?name=" + id, "90%", "90%", function (layero) {
// 				//let iframeWin = window[layero.find("iframe")[0]["name"]];
// 				//iframeWin.initForm(data);
// 			}, function () {
// 				userTable.reload();
// 				userTablebd.reload();
// 			})
// 		}
		
		
						window.config = function(obj) {
						layer.open({
                        type: 2,
                        maxmin: true,
                        title: '配置',
                        shade: 0.1,
                        area: ['80%', '80%'],
						content: 'config?name='+obj
					});
				}
		
		

		function uninstall(id) {
			layer.confirm("确定要卸载安装吗？", function () {
			    	layer.close(id);
			let loading = layer.load();
				 $.ajax({
                            url:'{:url('uninstall')}',
                            data:{id:id},
                            dataType: 'json',
                            type: 'get',
                            success: function(res) {
                                   layer.close(loading);
                                //判断有没有权限
                                if(res && res.code==999){
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        time: 2000, 
                                    })
                                    return false;
                                }else if (res.code==1) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function() {
                                      table.reload('tableIdbd');
                                      table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2,
                                        time: 1000
                                    });
                                }
                            }
                        }).fail(function (error) {
					console.log(error)
				});
			})
		}
		function state(id) {
			layer.confirm("确定执行此操作吗？", function () {
			  $.ajax({
                            url:'{:url('state')}',
                             data:{id:id},
                            dataType: 'json',
                            type: 'get',
                            success: function(res) {
                                //  layer.close(loading);
                                //判断有没有权限
                                if(res && res.code==999){
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        time: 2000, 
                                    })
                                    return false;
                                }else if (res.code==1) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000
                                    }, function() {
                                      table.reload('tableIdbd');
                                      table.reload('tableId');
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 2,
                                        time: 1000
                                    });
                                }
                            }
                        }).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>


<script type="text/html" id="statusTpl">

			{{# if(d.install==1){ }}
			<input type="checkbox" value="{{d.name}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>

			{{# } }}

</script>
{/block}